package com.ml.nio.channel;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
 * FileChannel案例4-拷贝文件transferFrom 方法
 * 实例要求:
 * 1) 使用 FileChannel(通道) 和 方法 transferFrom ，完成文件的拷贝
 * 2) 拷贝一张图片
 *
 * @author xr-8
 * @date 2022-01-08
 * @time 23:19
 */
public class FileChannel04 {

    public static void main(String[] args) throws IOException {

        FileInputStream fileInputStream = new FileInputStream("G://ScreenShot2021_0908_155810778.jpg");
        FileChannel sourceChannel = fileInputStream.getChannel();

        FileOutputStream fileOutputStream = new FileOutputStream("./4.jpg");
        FileChannel destChannel = fileOutputStream.getChannel();

        //使用transferForm完成拷贝
        destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
        //使用transferTo也可以完成拷贝
        //sourceChannel.transferTo(0, sourceChannel.size(), destChannel);

        //关闭相关通道和流
        sourceChannel.close();
        destChannel.close();
        fileInputStream.close();
        fileOutputStream.close();
    }
}
